1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module soup.Buffer;
26 
27 private import glib.Bytes;
28 private import glib.ConstructionException;
29 private import glib.MemorySlice;
30 public  import glib.c.types;
31 private import gobject.ObjectG;
32 private import linker.Loader;
33 private import soup.c.functions;
34 public  import soup.c.types;
35 
36 
37 /**
38  * A data buffer, generally used to represent a chunk of a
39  * #SoupMessageBody.
40  * 
41  * @data is a #char because that's generally convenient; in some
42  * situations you may need to cast it to #guchar or another type.
43  */
44 public final class Buffer
45 {
46 	/** the main Gtk struct */
47 	protected SoupBuffer* soupBuffer;
48 	protected bool ownedRef;
49 
50 	/** Get the main Gtk struct */
51 	public SoupBuffer* getBufferStruct(bool transferOwnership = false)
52 	{
53 		if (transferOwnership)
54 			ownedRef = false;
55 		return soupBuffer;
56 	}
57 
58 	/** the main Gtk struct as a void* */
59 	protected void* getStruct()
60 	{
61 		return cast(void*)soupBuffer;
62 	}
63 
64 	/**
65 	 * Sets our main struct and passes it to the parent class.
66 	 */
67 	public this (SoupBuffer* soupBuffer, bool ownedRef = false)
68 	{
69 		this.soupBuffer = soupBuffer;
70 		this.ownedRef = ownedRef;
71 	}
72 
73 	~this ()
74 	{
75 		if ( Linker.isLoaded(LIBRARY_SOUP[0]) && ownedRef )
76 			soup_buffer_free(soupBuffer);
77 	}
78 
79 	/**
80 	 * the data
81 	 */
82 	public @property ubyte [] data() {
83 		return cast(ubyte [])soupBuffer.data[0..getArrayLength(cast(ubyte *)soupBuffer.data)];
84 	}
85 
86 	/** Ditto */
87 	public @property void data(ubyte [] value) {
88 		soupBuffer.data = cast(void *)value.ptr;
89 	}
90 
91 	/**
92 	 */
93 
94 
95 	/**
96 	 * length of @data
97 	 */
98 	public @property size_t length()
99 	{
100 		return soupBuffer.length;
101 	}
102 
103 	/** Ditto */
104 	public @property void length(size_t value)
105 	{
106 		soupBuffer.length = value;
107 	}
108 
109 	/** */
110 	public static GType getType()
111 	{
112 		return soup_buffer_get_type();
113 	}
114 
115 	/**
116 	 * Creates a new #SoupBuffer containing @length bytes from @data.
117 	 *
118 	 * Params:
119 	 *     use = how @data is to be used by the buffer
120 	 *     data = data
121 	 *
122 	 * Returns: the new #SoupBuffer.
123 	 *
124 	 * Throws: ConstructionException GTK+ fails to create the object.
125 	 */
126 	public this(SoupMemoryUse use, ubyte[] data)
127 	{
128 		auto __p = soup_buffer_new(use, data.ptr, cast(size_t)data.length);
129 
130 		if(__p is null)
131 		{
132 			throw new ConstructionException("null returned by new");
133 		}
134 
135 		this(cast(SoupBuffer*) __p);
136 	}
137 
138 	/**
139 	 * Creates a new #SoupBuffer containing @length bytes from @data.
140 	 *
141 	 * This function is exactly equivalent to soup_buffer_new() with
142 	 * %SOUP_MEMORY_TAKE as first argument; it exists mainly for
143 	 * convenience and simplifying language bindings.
144 	 *
145 	 * Params:
146 	 *     data = data
147 	 *
148 	 * Returns: the new #SoupBuffer.
149 	 *
150 	 * Since: 2.32
151 	 *
152 	 * Throws: ConstructionException GTK+ fails to create the object.
153 	 */
154 	public this(char[] data)
155 	{
156 		auto __p = soup_buffer_new_take(data.ptr, cast(size_t)data.length);
157 
158 		if(__p is null)
159 		{
160 			throw new ConstructionException("null returned by new_take");
161 		}
162 
163 		this(cast(SoupBuffer*) __p);
164 	}
165 
166 	/**
167 	 * Creates a new #SoupBuffer containing @length bytes from @data. When
168 	 * the #SoupBuffer is freed, it will call @owner_dnotify, passing
169 	 * @owner to it. You must ensure that @data will remain valid until
170 	 * @owner_dnotify is called.
171 	 *
172 	 * For example, you could use this to create a buffer containing data
173 	 * returned from libxml without needing to do an extra copy:
174 	 *
175 	 * <informalexample><programlisting>
176 	 * xmlDocDumpMemory (doc, &xmlbody, &len);
177 	 * return soup_buffer_new_with_owner (xmlbody, len, xmlbody,
178 	 * (GDestroyNotify)xmlFree);
179 	 * </programlisting></informalexample>
180 	 *
181 	 * In this example, @data and @owner are the same, but in other cases
182 	 * they would be different (eg, @owner would be a object, and @data
183 	 * would be a pointer to one of the object's fields).
184 	 *
185 	 * Params:
186 	 *     data = data
187 	 *     owner = pointer to an object that owns @data
188 	 *     ownerDnotify = a function to free/unref @owner when
189 	 *         the buffer is freed
190 	 *
191 	 * Returns: the new #SoupBuffer.
192 	 *
193 	 * Throws: ConstructionException GTK+ fails to create the object.
194 	 */
195 	public this(ubyte[] data, void* owner, GDestroyNotify ownerDnotify)
196 	{
197 		auto __p = soup_buffer_new_with_owner(data.ptr, cast(size_t)data.length, owner, ownerDnotify);
198 
199 		if(__p is null)
200 		{
201 			throw new ConstructionException("null returned by new_with_owner");
202 		}
203 
204 		this(cast(SoupBuffer*) __p);
205 	}
206 
207 	/**
208 	 * Makes a copy of @buffer. In reality, #SoupBuffer is a refcounted
209 	 * type, and calling soup_buffer_copy() will normally just increment
210 	 * the refcount on @buffer and return it. However, if @buffer was
211 	 * created with #SOUP_MEMORY_TEMPORARY memory, then soup_buffer_copy()
212 	 * will actually return a copy of it, so that the data in the copy
213 	 * will remain valid after the temporary buffer is freed.
214 	 *
215 	 * Returns: the new (or newly-reffed) buffer
216 	 */
217 	public Buffer copy()
218 	{
219 		auto __p = soup_buffer_copy(soupBuffer);
220 
221 		if(__p is null)
222 		{
223 			return null;
224 		}
225 
226 		return ObjectG.getDObject!(Buffer)(cast(SoupBuffer*) __p, true);
227 	}
228 
229 	/**
230 	 * Frees @buffer. (In reality, as described in the documentation for
231 	 * soup_buffer_copy(), this is actually an "unref" operation, and may
232 	 * or may not actually free @buffer.)
233 	 */
234 	public void free()
235 	{
236 		soup_buffer_free(soupBuffer);
237 		ownedRef = false;
238 	}
239 
240 	/**
241 	 * Creates a #GBytes pointing to the same memory as @buffer. The
242 	 * #GBytes will hold a reference on @buffer to ensure that it is not
243 	 * freed while the #GBytes is still valid.
244 	 *
245 	 * Returns: a new #GBytes which has the same content
246 	 *     as the #SoupBuffer.
247 	 *
248 	 * Since: 2.40
249 	 */
250 	public Bytes getAsBytes()
251 	{
252 		auto __p = soup_buffer_get_as_bytes(soupBuffer);
253 
254 		if(__p is null)
255 		{
256 			return null;
257 		}
258 
259 		return new Bytes(cast(GBytes*) __p, true);
260 	}
261 
262 	/**
263 	 * This function exists for use by language bindings, because it's not
264 	 * currently possible to get the right effect by annotating the fields
265 	 * of #SoupBuffer.
266 	 *
267 	 * Params:
268 	 *     data = the pointer
269 	 *         to the buffer data is stored here
270 	 *
271 	 * Since: 2.32
272 	 */
273 	public void getData(out ubyte[] data)
274 	{
275 		ubyte* outdata;
276 		size_t length;
277 
278 		soup_buffer_get_data(soupBuffer, &outdata, &length);
279 
280 		data = outdata[0 .. length];
281 	}
282 
283 	/**
284 	 * Gets the "owner" object for a buffer created with
285 	 * soup_buffer_new_with_owner().
286 	 *
287 	 * Returns: the owner pointer
288 	 */
289 	public void* getOwner()
290 	{
291 		return soup_buffer_get_owner(soupBuffer);
292 	}
293 
294 	/**
295 	 * Creates a new #SoupBuffer containing @length bytes "copied" from
296 	 * @parent starting at @offset. (Normally this will not actually copy
297 	 * any data, but will instead simply reference the same data as
298 	 * @parent does.)
299 	 *
300 	 * Params:
301 	 *     offset = offset within @parent to start at
302 	 *     length = number of bytes to copy from @parent
303 	 *
304 	 * Returns: the new #SoupBuffer.
305 	 */
306 	public Buffer newSubbuffer(size_t offset, size_t length)
307 	{
308 		auto __p = soup_buffer_new_subbuffer(soupBuffer, offset, length);
309 
310 		if(__p is null)
311 		{
312 			return null;
313 		}
314 
315 		return ObjectG.getDObject!(Buffer)(cast(SoupBuffer*) __p, true);
316 	}
317 }